<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="description" content="先咨询，后用药，速递易" />
<title>自在生活</title>
<jsp:include page="comm.jsp"></jsp:include>
<%-- <script type="text/javascript" src="<%=basePath%>/mpjs/manage/sysMsg.js"></script> --%>
<script type="text/javascript">
$(function(){
	//查询所有的系统消息
	$("#sysMsgInfo").datagrid({
		url:basePath + '/up/getPicList',
		fitColumns:true,
		fit:true,
		rownumbers:true,
		singleSelect:true, 
		pagination:true,
		pageSize:20,
		pageNumber:1,
		modal : true,
		toolbar:[{
			text:"添加图片",
			iconCls:'icon-add',
			handler:function(){
				$('#add_msg_dialog').dialog('open');
			}
		},'-',{
			text:"删除图片",
				iconCls:'icon-remove',
				handler:function(){
					delPic();
				}
		},'-',{
			text:"禁用图片",
				iconCls:'icon-forbidden',
				handler:function(){
					updateStatus(0);
				}
		},'-',{
			text:"启用图片",
			iconCls:'icon-start',
			handler:function(){
				updateStatus(1);
			}
		}],
		columns: [[
			{field:'id',hidden:true,width:120,align:'center'},
			{field:'type',title:'图片类型',width:120,align:'center',
				formatter:function(value){
					switch(value){
						case 0 : return '轮播图片';
						case 1 : return '商品图片';
						case 2 : return '关于我们';
						case 3 : return 'app下载';
						case 4 : return '关于我们轮播';
						case 5 : return '关于我们公众号二维码';
						case 6 : return '关于我们线下会所';
					}				
				}
			},
			{field:'path',title:'图片路径',width:120,align:'center'},
			{field:'action',title:'图片链接',width:120,align:'center'},
			{field:'isAble',title:'是否可用',width:120,align:'center',
				formatter:function(value){
					return value == 0?'不可用':'可用';
				} 
			},
			{field:'createTime',title:'上传时间',width:120,align:'center',
				formatter:function(value){
					var date = new Date(value);
					var year = date.getFullYear();
					var month = date.getMonth()+1;
					var day = date.getDate()>9?date.getDate():'0'+date.getDate();
					var hour = date.getHours()>9?date.getHours():'0'+date.getHours();
					var minutes = date.getMinutes()>9?date.getMinutes():'0'+date.getMinutes();
					var second = date.getSeconds()>9?date.getSeconds():'0'+date.getSeconds();
					return year+'-'+month+'-'+day+' '+hour+':'+minutes+':'+second;
				}
			}
		]]
	});
	
	$('#add_msg_form').form({    
	    url:basePath+"/up/uploadPic",
	    onSubmit:function(param){
	    	var coverName = $("#sourceFile").filebox('getValue');
	    	if(coverName != null){
	    		$("#add_cover").val(coverName);
	    	}
	    	return $('#add_msg_form').form('validate');
	    },
	    success:function(data){
	    	data = $.parseJSON(data);
	    	if(data.resultCode == 0){
	    		$('#add_msg_dialog').dialog('close');
	    		$("#sysMsgInfo").datagrid('reload');
	    	}
	    	$.messager.show({title:"warning",msg:data.msg,timeout:5000,showType:'slide'}); 
	    }    
	});
	
	$('#add_msg_dialog').dialog({    
	    title: 'add picture', 
	    closed: true,    
	    cache: false, 
		width: '30%',
		height: '35%',
	    modal: true ,
	    buttons : [ {
			text : 'confirm',
			id : 'submitBTN',
			iconCls : 'icon-save',
			handler : function() {
				$("#add_msg_form").submit();
			}
		}, {
			text : 'close',
			iconCls : 'icon-remove',
			handler : function() {
				$('#add_msg_dialog').dialog('close');
			}
		} ]
	});
});

/* 删除图片 */
function delPic(){
	var grid = $("#sysMsgInfo").datagrid('getSelected');
           if(grid == null){
               $.messager.show({title:"提示信息",msg:'请先选择图片记录!',timeout:5000,showType:'slide'});
               return false;
           }
           window.parent.$.messager.confirm('提示', '您确定要删除么?', function(r){
               if(r){
                   var picId = grid.picId;
                   var path = grid.path;
                   $.ajax({
                       url : basePath + '/up/deletePic',
                       type : 'post',
                       data : {
                           'picId':picId,
                           'picPath':path
                       },
                       success : function(data){
                       console.info(data);
                           if(data.resultCode == 0){
                               $("#sysMsgInfo").datagrid('reload');
                            }
                           $.messager.show({title:"提示信息",msg:data.msg,timeout:5000,showType:'slide'});
                       }
                   });
               }
           });
}
/* 更改状态 */
function updateStatus(isAble){
	var grid = $("#sysMsgInfo").datagrid('getSelected');
    if(grid == null){
        $.messager.show({title:"提示信息",msg:'请先选择图片记录!',timeout:5000,showType:'slide'});
        return false;
    }
     var picId = grid.picId;
     $.ajax({
         url : basePath + '/up/updatePicStatus',
         type : 'post',
         data : {
             'picId':picId,
             'isAble':isAble
         },
         success : function(data){
         console.info(data);
             if(data.resultCode == 0){
                 $("#sysMsgInfo").datagrid('reload');
              }
             $.messager.show({title:"提示信息",msg:data.msg,timeout:5000,showType:'slide'});
         }
     });
}
</script>
</head>
<body>
	<!-- 查询系统消息 -->
	<div id="sysMsgInfo"></div>

	<div id="add_msg_dialog">
		<form id="add_msg_form" enctype="multipart/form-data" method="post">
			<table class="table">
				<tr>
					<th>图片类型:</th>
					<td><select id="cc" class="easyui-combobox" name="type"
						style="width:200px;">
							<option value="0">轮播图片</option>
							<option value="1">商品图片</option>
							<option value="2">关于我们</option>
							<option value="3">app下载</option>
							<option value="4">关于我们轮播</option>
							<option value="5">关于我们公众号二维码</option>
							<option value="6">关于我们线下会所</option>
					</select></td>
				</tr>
				<tr>
					<th>图片地址:</th>
					<td><input id="add_cover" type="text" name="cover"
						style="display: none;"> <input class="easyui-filebox"
						id="sourceFile" name="sourceFile" class="easyui-validatebox"
						required="true" buttonIcon="icon-search" buttonText="choose"
						style="width:200px"> <span><font color="red">
								* 小于 3M</font></span></td>
				</tr>
				<tr>
					<th>
						链接地址:
					</th>
					<td>
					 	<textarea name="action" style="width:200px;"></textarea>  
					</td>
				</tr>
				<tr>
					<th>是否启用:</th>
					<td><select id="cc" class="easyui-combobox" name="isAble"
						style="width:200px;">
							<option value="1">启用</option>
							<option value="0">禁用</option>
					</select></td>
				</tr>
				<tr>
					<th>
						排序:
					</th>
					<td>
					 	<input type="text" style="width:200px;" class="easyui-numberbox" name="order" required="true" data-options="min:0,max:100,precision:0"><span><font color="red">*</font></span>  
					</td>
				</tr>
			</table>
		</form>
	</div>
</body>
</html>
